package com.demo.demo.controller;

import com.demo.demo.bean.QueryInfo;
import com.demo.demo.bean.Warehouse;
import com.demo.demo.dao.WarehouseDao;
import com.demo.demo.bean.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.demo.demo.bean.PageInfo;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseDao warehouseDao;

    // 获取所有仓库地址
    @GetMapping("/locations")
    public ResponseResult<List<String>> getWarehouseLocations() {
        List<String> locations = warehouseDao.getWarehouseLocations();
        return new ResponseResult<>(200, "获取成功", locations,0);
    }

    // 根据仓库地址查询商品
    @GetMapping("/products/{location}")
    public ResponseResult<List<Warehouse>> getProductsByLocation(@PathVariable String location) {
        List<Warehouse> products = warehouseDao.getProductsByLocation(location);
        return new ResponseResult<>(200, "获取成功", products,0);
    }
    @PostMapping("/products")
    public PageInfo<Warehouse> getProductsByCriteria(@RequestBody QueryInfo queryInfo) {
        String varietyType = queryInfo.getVarietyType();
        String location = queryInfo.getLocation();
        int pageNum = queryInfo.getPageNum();
        int pageSize = queryInfo.getPageSize();
        int pageStart = (pageNum - 1) * pageSize;

        List<Warehouse> products = warehouseDao.findPagedByVarietyAndLocation(varietyType, location, pageStart, pageSize);
        int total = warehouseDao.countByVarietyAndLocation(varietyType, location);

        return new PageInfo<>(products, total, pageNum, pageSize);
    }




}
